﻿using MyNeighbourhood.Data;
using MyNeighbourhood.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyNeighbourhood.Managers
{
    public class LocationsManager : BaseManager
    {
        public Location CreateLocation(LocationModel model)
        {
            try
            {
                var repeated = dbContext.Locations.FirstOrDefault(x => x.Street == model.Street && x.Number == model.Number);
                if (repeated != null)
                {
                    return repeated;
                }

                var location = new Location();
                location.Number = model.Number;
                location.Street = model.Street;

                var result = dbContext.Locations.Add(location);

                dbContext.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}